﻿using System;

namespace Kaleida.ServiceMonitor.Model.Statistics
{
    public class ResponseTimePercentile
    {
        private readonly decimal percentage;
        private readonly TimeSpan? value;

        public ResponseTimePercentile(decimal percentage, TimeSpan? value)
        {
            if (percentage < 0 || percentage > 100)
                throw new ArgumentOutOfRangeException("percentage", percentage, "Must be between 0 and 100");

            this.percentage = percentage;
            this.value = value;
        }

        public decimal Percentage
        {
            get { return percentage; }
        }

        public TimeSpan? Value
        {
            get { return value; }
        }

        public override string ToString()
        {
            var durationString = value == null ? "n/a" : value.Value.TotalMilliseconds.ToString("0");
            return string.Format("{0:D2}% of requests received within {1} ms", percentage, durationString);
        }
    }
}
